<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC3026 – In POSIX sh, `^` in place of `!` in glob bracket expressions is undefined.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC3026 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC3026">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="in-posix-sh--in-place-of--in-glob-bracket-expressions-is-undefined">In
POSIX sh, <code>^</code> in place of <code>!</code> in glob bracket
expressions is undefined.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC3026.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> foo-[^0]<span class="pp">*</span>.jpg</span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC3026.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> foo-[!0]<span class="pp">*</span>.jpg</span></code></pre></div>
<p>or if the intention is to match <code>^</code> literally then either
do not put it immediately after <code>[</code> or quote it by backslash
<code>\</code></p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC3026.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> foo-[0^]<span class="pp">*</span>.jpg foo-[\^0]<span class="pp">*</span>.jpg</span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>[^c]</code> is frequently used in most regular expression
variants to mean "any character except <code>c</code>". Ksh and Bash
adopted it for globs as well.</p>
<p>However, strictly speaking, the only range complement syntax
guaranteed to be supported across shells is <code>[!c]</code>. POSIX
explicitly states that behavior of <code>[^c]</code> is unspecified.
Dash used to support <code>[^c]</code> when compiled with
<code>fnmatch</code> and <code>glob</code> from glibc, but it was
considered as a <a
href="https://lore.kernel.org/dash/09fd70edcf08c75b4f9f2e158e3e6c0832d35d90.camel@scientia.org/t/#u">bug</a>
and fixed in version 0.5.12. Busybox has a similar <a
href="https://bugs.busybox.net/show_bug.cgi?id=14516">bug 14516</a>.</p>
<p>In zsh interactive sessions <code>echo foo-[!0]*.jpg</code> may cause
<code>zsh: event not found: 0</code> error due to attempt of history
expansion. BASH however suppresses history expansion when <code>!</code>
is first character in square brackets.</p>
<p>Discrepancy of negation syntax in regular expressions and shell
pattern matching appeared in early UNIX versions. Pipeline syntax in
shells had not settled yet, some terminals had no <code>|</code> key, so
<code>^</code> was used there. That is why in some shells exclamation
mark <code>!</code> was chosen instead of circumflex (caret)
<code>^</code> symbol for negation in globs. For more details related to
shell history see <a
href="https://inbox.vuxu.org/tuhs/20030126014003.GA3818@sydney.worldwide.lemis.com/T/#u">"The
<code>^</code> <code>=</code> <code>|</code> ?" thread</a> in the Unix
Heritage Society mailing list or <a
href="https://www.in-ulm.de/~mascheck/bourne/">Sven Mascheck. The
Traditional Bourne Shell Family. History and Development</a> page.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If you only intend to target shells that supports this feature, you
can change the shebang to a shell that guarantees support, or <a
href="ignore.html">ignore</a> this warning. Or just rewrite it to be on the
technically correct side.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_01">2.13.1
Patterns Matching a Single Character</a> in the Shell and Utilities
(XCU) volume of POSIX.1-2017.</li>
<li><a href="https://www.austingroupbugs.net/view.php?id=1558">0001558:
require <code>[^...]</code> in addition to <code>[!...]</code> for
bracket expression negation</a> POSIX enhancement request was
rejected.</li>
<li>Help by adding links to BashFAQ, StackOverflow, man pages, POSIX,
etc!</li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


